package com.qiaoanju.back.service.impl.system;

import com.qiaoanju.back.dao.system.IGroupDao;
import com.qiaoanju.back.domain.system.GroupModel;
import com.qiaoanju.back.domain.system.MenuModel;
import com.qiaoanju.back.service.system.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by eversec
 * on 2017/9/23.
 */
@Service
public class GroupServiceImpl implements IGroupService {

    @Autowired
    private IGroupDao dao ;

    /**
     * @param group_id
     * @return
     */
    public GroupModel getGroupById(Integer group_id) {
        return dao.getGroupById(group_id);
    }

    public int insert(GroupModel group) {
        return dao.insert(group);
    }

    /**
     * 获取全部部门
     *
     * @param root
     * @return
     */
    public List<GroupModel> getAllGroups(int root) {

        List<GroupModel> firstLayer =  getChildsByRoot(0);
        for (GroupModel group: firstLayer) {
            setChildGroups(group);
        }
        return firstLayer;
    }
    /**
     * 设置子部门
     * @param parent
     */
    private void setChildGroups(GroupModel parent) {

        //
        List<GroupModel> childGroups = getChildsByRoot(parent.getId());
        if (childGroups!= null && childGroups.size() > 0) {//有子部门
            parent.setChildGroups(childGroups);
            for (GroupModel group: childGroups ) {
                //递归设置子菜单
                setChildGroups(group);
            }
        }
    }
    public List<GroupModel> getChildsByRoot(int root) {

        return dao.getChildsByRoot(root);
    }

    /**
     * @param group
     * @return
     */
    public boolean edit(GroupModel group) {
        return dao.edit(group);
    }
}
